/**
 * Created with CLion.
 * Description: 交换操作中使用 std::lock() 和 std::lock_guard
 * User: loong
 * Date: 2020-09-09
 * Time: 23:23
 */
#include <iostream>
#include <mutex>
using namespace std;

class some_big_object;

void swap(some_big_object &lhs, some_big_object &rhs);

class X
{
private:
    some_big_object some_detail;
    
    std::mutex m;

public:
    X(some_big_object const &sd)
        : some_detail(sd)
    { }
    
    friend void swap(X &lhs, X &rhs)
    {
        if (&lhs == &rhs)
            return;
        std::lock(lhs.m, rhs.m); // 1
        std::lock_guard<std::mutex> lock_a(lhs.m, std::adopt_lock); // 2
        std::lock_guard<std::mutex> lock_b(rhs.m, std::adopt_lock); // 3
        swap(lhs.some_detail, rhs.some_detail);
    }
};

int main()
{
    
    return 0;
}